//
//  BKNoteEidtVC.m
//  lg_zhinengbeikao_english
//
//  Created by 盛超 on 2019/1/10.
//  Copyright © 2019年 zhukai. All rights reserved.
//

#import "BKNoteEidtVC.h"
#import "BKAddPhotoView.h"

@interface BKNoteEidtVC ()<UITextViewDelegate,BKAddPhotoViewDelegate>

@property (nonatomic,strong) UITextView *noteTextView;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) BKAddPhotoView *addPhotoView;

@end

@implementation BKNoteEidtVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"笔记";
    // Do any additional setup after loading the view.
    [self setupUI];
    [self addLeftBackBarButtonItem];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)setupUI {
    //#1
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    
    UILabel *label = [[UILabel alloc] init];
    [self.scrollView addSubview:label];
    
    self.noteTextView = [[UITextView alloc] init];
    [self.scrollView addSubview:self.noteTextView];
    
    self.addPhotoView = [[BKAddPhotoView alloc] init];
    self.addPhotoView.delegate = self;
    [self.scrollView addSubview:self.addPhotoView];
    
    //#2
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
        make.bottom.equalTo(self.addPhotoView).offset(0);
    }];
    
    label.text = @"这道题的笔记";
    label.font = FONT(17);
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.top.equalTo(self.scrollView).offset(15);
        make.height.mas_equalTo(16);
    }];
  
    
    self.noteTextView.font = FONT(17);
    self.noteTextView.delegate = self;
    [self.noteTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(5);
        make.right.equalTo(self.view).offset(-5);
        make.top.equalTo(label.mas_bottom);
    }];
    
    NSInteger space = 3;
    CGFloat width = SCREEN_WIDTH - 20;
    CGFloat itemW = (width - 2*space)/3;
    [self.addPhotoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.top.equalTo(self.noteTextView.mas_bottom).offset(10);
        make.height.mas_equalTo(itemW);
    }];
    
    [self updateSubViewConstraints];
    
    NSMutableArray *images = [NSMutableArray array];
    for (NSInteger i = 0; i<3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Guide_image%ld",i+1]];
        [images addObject:image];
    }
    self.addPhotoView.images = images;
    
}

- (void)updateSubViewConstraints {
    NSString *text = @"Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.Do any additional setup after loading the view, typically from a nib.";
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10;// 字体的行间距
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:17],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    self.noteTextView.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
    CGFloat h = [self getSizeH:text];
    [self.noteTextView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(h);
    }];
    
}

- (CGFloat)getSizeH:(NSString*)text {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10;// 字体的行间距
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:17],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
    CGSize size = [attributedText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    
    return size.height+20;
}

#pragma mark - textViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    
    CGFloat h = [self getSizeH:textView.text];
    [self.noteTextView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(h);
    }];
    
}

#pragma mark - BKAddPhotoViewDelegate
- (void)refreshPhototView:(BKAddPhotoView *)addPhotoView PhotoCount:(NSInteger)count {
    
    NSInteger space = 3;
    CGFloat width = SCREEN_WIDTH - 20;
    CGFloat itemW = (width - 2*space)/3;
    
    if (count<9) {
        [addPhotoView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(itemW+(count/3)*itemW+10);
        }];
    }
}

@end
